package com.example.administrator.printoutindemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.adapter.RuKuSuccessAdapter;
import com.example.administrator.printoutindemo.base.BaseFragment;
import com.example.administrator.printoutindemo.bean.RukuSuccessBean;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseConstants;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;

/**
 * Created by noc on 2018/10/25.
 */

public class RuKuSuccessFragment extends BaseFragment implements RuKuSuccessAdapter.IRuKuSuccessAdapter {
    private View view;
    private EditText et_ddh, et_ddmc, et_wlmc, et_gys, et_cwmc;
    private ListView lv_liebiao;
    private List<RukuSuccessBean> mmlist;
    private RuKuSuccessAdapter mAdapter;
    private TextView tv_chaxun;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.rukusuccesslist, null);
        ToolbarUtils.initToolbarTitleNoBack(view, this, getString(R.string.ru_ku_cheng_gong_ji_lu));

        et_ddh = view.findViewById(R.id.et_ddh);
        et_ddmc = view.findViewById(R.id.et_ddmc);
        et_wlmc = view.findViewById(R.id.et_wlmc);

        et_gys = view.findViewById(R.id.et_gys);
        et_cwmc = view.findViewById(R.id.et_cwmc);
        tv_chaxun = view.findViewById(R.id.tv_chaxun);
        lv_liebiao = view.findViewById(R.id.lv_liebiao);
        mmlist = new ArrayList<>();
        mAdapter = new RuKuSuccessAdapter(getActivity(), mmlist,this);
        lv_liebiao.setAdapter(mAdapter);
        tv_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LoadingDialogUtil.showGifdialog2(getFragmentManager(), R.drawable.public_loading);
                instoreHistoryForAndroid();
            }
        });
        return view;
    }
    private void instoreHistoryForAndroid() {


        String methodName = "InstoreHistoryForAndroid";

        SoapParams params = new SoapParams();
        params.put("uid", SPUtil.getString(getContext(), "iduser", ""));//用户id
        params.put("projectName", et_ddmc.getText().toString());//项目名称
        params.put("supplierName", et_gys.getText().toString());//供应商名称
        params.put("material", et_wlmc.getText().toString());//物料名称
        params.put("purchaseid", et_ddh.getText().toString());//订单号
        params.put("spname", et_cwmc.getText().toString());//仓位名称

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mAdapter.shua();
                    Log.e("入库成功记录======", result);
                    List<RukuSuccessBean> rukuSuccessBeen = new Gson().fromJson(result, new TypeToken<List<RukuSuccessBean>>() {
                    }.getType());
                    mAdapter.addAll(rukuSuccessBeen);
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void modifyRenark(RukuSuccessBean rukuSuccessBean, String remark) {

    }
}
